import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class MyLRU<K, V> extends LinkedHashMap<K, V> {
    private final int MAX_ENTRIES;

    public MyLRU(int initialCapcity, int threshhold, boolean accessOrder) {
        super(initialCapcity, 0.75f, accessOrder);
        this.MAX_ENTRIES = threshhold;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > MAX_ENTRIES;
    }


    public static void main(String[] args) {
        MyLRU<String, Integer> map = new MyLRU<>(0, 5, true);
        for (int i = 0; i < 10; i++) {
            map.put(String.valueOf(i), i);
            if (i > 4)
                map.get("2");
        }
        System.out.println("--Original situation--");
        for (Map.Entry e : map.entrySet()) {
            System.out.println(e.getKey() + " = " + e.getValue());
        }

        map.get("6");
        System.out.println("--After acess--");
        for (Map.Entry e : map.entrySet()) {
            System.out.println(e.getKey() + " = " + e.getValue());
        }
        System.out.println("--After expire--");

        map.put("A", 10);
        for (Map.Entry e : map.entrySet()) {
            System.out.println(e.getKey() + " = " + e.getValue());
        }
        System.out.println(map.size());
    }

}

